import {
	Injectable,
	ExecutionContext,
	UnauthorizedException,
} from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";

@Injectable()
export class JwtAuthGuard extends AuthGuard("jwt") {
	canActivate(context: ExecutionContext) {
		return super.canActivate(context);
	}

	handleRequest(err, user, info) {
		if (err || !user) {
			if (info && info.name === "TokenExpiredError") {
				throw new UnauthorizedException("Token已过期");
			}
			if (info && info.name === "JsonWebTokenError") {
				throw new UnauthorizedException("无效的Token");
			}
			throw err || new UnauthorizedException("认证失败");
		}
		return user;
	}
}
